package com.kwan.xframe.mvp.model;

import android.util.Log;

import com.kwan.xframe.api.DataSubscriberListener;
import com.kwan.xframe.mvp.presenter.IBasePresenter;

import java.util.HashMap;
import java.util.Map;

import androidx.collection.ArrayMap;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * Model 层父类
 * <p>
 * Created by Mr.Kwan on 2016-10-8.
 */

public abstract class BaseModel implements DataSubscriberListener<Object> {

	protected IBasePresenter mIBasePresenter;
	private ArrayMap<Integer, CompositeDisposable> netManager = new ArrayMap<>();

	public BaseModel(IBasePresenter iBasePresenter) {
		mIBasePresenter = iBasePresenter;
	}

	@Override
	public void onDataCompleted(int vocational_id, HashMap<String, Object> exData) {

	}

	@Override
	public void onDataError(int vocational_id, HashMap<String, Object> exData, Throwable throwable) {
		mIBasePresenter.onDataError(vocational_id, exData, throwable);
		Log.e("kwan","omDataError");
	}

	@Override
	public void onDataNext(int vocational_id, HashMap<String, Object> exData, Object data) {
		mIBasePresenter.onDataSuccess(vocational_id, exData, data);
	}


	//为了避免错误的取消了，key 建议使用 packageName + className
	public void addDisposable(Integer key, Disposable disposable) {

		if (netManager.containsKey(key)) {
			netManager.get(key).add(disposable);
		} else {
			CompositeDisposable compositeDisposable = new CompositeDisposable();
			compositeDisposable.add(disposable);
			netManager.put(key, compositeDisposable);
		}
	}


	public void clearDisposable() {

		for (Map.Entry<Integer, CompositeDisposable> entry : netManager.entrySet())
			netManager.get(entry.getKey()).clear();
	}

	public void removeDisposable(Integer key) {
		if (netManager.containsKey(key)) {
			CompositeDisposable compositeDisposable = netManager.get(key);
			compositeDisposable.clear();
		}
	}


}

